from http.client import responses

import requests
import re

from sqlparse.engine.grouping import group

# url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
# response_login = requests.get(url)
# # print(response.status_code)
# # print(response_login.text)
# response_cookies = response_login.cookies
# # print(response_cookies)
# csrfmiddlewaretoken = re.search('<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">', response_login.text).group(1)
#
# response = requests.post(url, data={
#     'username': 'admin',
#     'password': '123456',
#     'csrfmiddlewaretoken': csrfmiddlewaretoken
# }, cookies=response_cookies)
#
# print(response.text)



url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
session = requests.Session()
response = session.get(url)
csrfmiddlewaretoken = re.search('<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">', response.text).group(1)

response = session.post(url, data={
    'username': 'admin',
    'password': '123456',
    'csrfmiddlewaretoken': csrfmiddlewaretoken,
})
print(response.text)


